package com.jjy.admin.service.echarts.impl;


import cn.hutool.json.JSONUtil;
import com.jjy.admin.domain.JobSpiderDTO;
import com.jjy.admin.domain.TaskJob;
import com.jjy.admin.mapper.CommonMapper;
import com.jjy.admin.mapper.JobMapper;
import com.jjy.admin.mapper.TaskJobMapper;
import com.jjy.admin.service.echarts.EchartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.*;

@Service
public class EchartServiceImpl implements EchartService {

    @Resource
    private JobMapper jobMapper;
    @Resource
    private TaskJobMapper taskJobMapper;
    @Resource
    private CommonMapper commonMapper;
    @Override
    public List getNumberData() {
//        1.今日新增job 2.job总数  3.待爬取数
        Long addCount = jobMapper.selectAddCount();
        Long totalCount = jobMapper.selectCount();
        Long futureCount = 0L;
        TaskJob taskJob = new TaskJob();
        taskJob.setTaskType(1);
        taskJob.setStatus(0);
        List<TaskJob> taskJobs = taskJobMapper.selectTaskList(taskJob);
        if (taskJobs != null && taskJobs.size() > 0) {
            for (TaskJob job : taskJobs) {
                String taskCondition = job.getTaskCondition();
                JobSpiderDTO jobSpiderCondition= JSONUtil.toBean(taskCondition,JobSpiderDTO.class);
                int pageNum = jobSpiderCondition.getPageNum();
                int pageSize = jobSpiderCondition.getPageSize();
                futureCount=futureCount+pageNum*pageSize;
            }
        }
        HashMap<String, Object> addCountMap = new HashMap<>();
        addCountMap.put("number", addCount);
        addCountMap.put("text", "今日新增数量");

        HashMap<String, Object> totalCountMap = new HashMap<>();
        totalCountMap.put("number", totalCount);
        totalCountMap.put("text", "总量");

        HashMap<String, Object> futureCountMap = new HashMap<>();
        futureCountMap.put("number", futureCount);
        futureCountMap.put("text", "即将新增数量");

        HashMap<String, Object> resultMap = new HashMap<>();
        List list = new ArrayList();
        list.add(addCountMap);
        list.add(totalCountMap);
        list.add(futureCountMap);
        return list;
    }

    @Override
    public List getConfigData() {

        return jobMapper.getConfigData();
    }

    @Override
    public Map getColumnarData() {
        ArrayList<Map> columnarData = commonMapper.getColumnarData();
        LinkedList<String> cityData = new LinkedList<>();
        LinkedList<String> cityDataMin = new LinkedList<>();
        LinkedList<String> cityDataMax = new LinkedList<>();
        for (Map<String,String> columnarDatum : columnarData) {
            String city = columnarDatum.get("city");
            String minSalary = columnarDatum.get("minSalary");
            String maxSalary = columnarDatum.get("maxSalary");
//        获取热门城市
            cityData.add(city);
//        获取热门城市最低薪资
            cityDataMin.add(minSalary);
//        获取热门城市最高薪资
            cityDataMax.add(maxSalary);
        }

        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("cityData", cityData.toArray());
        resultMap.put("cityDataMin", cityDataMin.toArray());
        resultMap.put("cityDataMax", cityDataMax.toArray());
        return resultMap;
    }

    @Override
    public List getGiftWords() {
        ArrayList<Object> resultList = new ArrayList<>();
        List<String> giftWords = jobMapper.getGiftWords();
        for (String giftWord : giftWords) {
        HashMap<String, String> map = new HashMap<>();
            map.put("name", giftWord);
            map.put("value", String.valueOf(new Random().nextInt(100)));
            resultList.add(map);
        }
        return resultList;
    }

    @Override
    public List getSalaryByExperience() {
        return jobMapper.getSalaryByExperience();
    }
    @Override
    public List getExperienceByExperience() {
        return jobMapper.getExperienceByExperience();
    }

    @Override
    public List getConeData() {
        return jobMapper.getConeData();
    }
}
